package com.kongwg.java2;

import com.kongwg.java1.Person;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * 获取当前运行时类的属性结构
 *
 * @author kongwg
 * @create 2020-12-24 20:28
 */
public class FieldTest {
    @Test
    public void test1() {
        //获取运行类和父类的public的属性
        Class<Person> clazz = Person.class;
        Field[] field = clazz.getFields();
        for (Field f : field){
            System.out.println(f);
        }

    }


    //获取当前运行类的声明的属性
    @Test
    public void test2(){
        Class<Person> clazz = Person.class;
        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields){
            //权限修饰符
            int modifiers = f.getModifiers();
            System.out.print(Modifier.toString(modifiers) + "\t");
            //数据类型
            Class<?> type = f.getType();
            System.out.print(type + "\t");
            //变量名
            String name = f.getName();
            System.out.println(name);
        }
    }
}
